﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Documents;
using CommonSearchModule;
using System.Data.SqlClient;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Index;
using System.Diagnostics;
using System.Configuration;
using System.IO;
using System.Text.RegularExpressions;

namespace CreateIndexFile
{
    public class SearchJiaoAnIndexFile : IndexFileBase
    {
        public SearchJiaoAnIndexFile()
            : base(ConfigurationManager.AppSettings["JiaoAn_IndexDocumentsPath"])
        { }


        #region Public Methods

        public override void Create()
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            StringBuilder sb;
            using (SqlConnection conn = new SqlConnection(connString))
            {
                sb = new StringBuilder();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                sb.Append(@"  
SELECT 'Res_TeachingPlan'+LTRIM(STR(a.ID)) as PrimaryKey, 
a.ID, 
a.Title as Name, 
a.UploadedTime as PackageTime,
a.Info, 
CategoryName='老师教案',
c.Name+'|'+d.Name as PropertyName

FROM dbo.Res_TeachingPlan a
inner JOIN dbo.Res_TeachingPlanRelation_R  b ON a.ID = b.PlanID
                                                and status=1
inner Join dbo.Common_BasePropValue c On b.GID=c.VID AND c.PID=1
inner Join dbo.Common_BasePropValue d On b.SID=d.VID AND d.PID=2
");
                comm.CommandText = sb.ToString();
                conn.Open();
                SqlDataReader reader = comm.ExecuteReader();

                int rowCount = 1;
                try
                {
                    while (reader.Read())
                    {
                        Search_Entity entity = new Search_Entity();
                        entity.PrimaryKey = reader["PrimaryKey"].ToString();
                        entity.ID = reader["ID"].ToString();
                        entity.Name = reader["Name"].ToString();
                        entity.Info = Regex.Replace(reader["Info"].ToString(), @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase);
                        entity.CategoryName = reader["CategoryName"].ToString();
                        entity.PackageTime = reader["PackageTime"].ToString();
                        entity.PropertyName = reader["PropertyName"].ToString();
                        if (!string.IsNullOrEmpty(entity.SearchUrlType))
                            entity.SearchUrlType = reader["SearchUrlType"].ToString();
                        //添加到索引
                        Document doc = InitDocument(entity);
                        writer.AddDocument(doc);

                        Console.SetCursorPosition(0, 0);
                        Console.WriteLine("执行次数：" + rowCount++.ToString());
                        Console.WriteLine("执行时间：" + watch.Elapsed.ToString());
                    }
                }
                finally
                {
                    reader.Close();
                }
            }

            writer.Optimize();
            writer.Close();

            TimeSpan _RunTime = new TimeSpan(DateTime.Now.Ticks);
            TimeSpan _RunTime2 = new TimeSpan(DateTime.Now.AddDays(-2).Ticks);
            TimeSpan ss = _RunTime.Subtract(_RunTime2);
            Console.WriteLine(ss.Hours);

            SetCreatePath();
        }

        #endregion Public Methods


    }
}
